package com.jrtc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.config.constants.PublicConstants;
import com.jrtc.base.entity.bo.*;
import com.jrtc.base.entity.vo.CatchBallRecordVo;
import com.jrtc.base.entity.vo.IdNameInfoVo;
import com.jrtc.common.StringHelper;
import com.jrtc.dao.*;
import com.jrtc.service.CatchBallRecordService;
import com.jrtc.service.DepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("catchBallRecordService")
public class CatchBallRecordServiceImpl implements CatchBallRecordService {
    @Resource
    private CatchBallRecordDao catchBallRecordDao;
    @Resource
    private PatientDao patientDao;
    @Resource
    private CatchBallRecordDetailsDao catchBallRecordDetailsDao;
    @Resource
    private ExerciserecordDao exerciserecordDao;


    /**
     * 添加患者抓小球记录
     * */
    @Override
    public CatchBallRecordVo insertRecord(CatchBallRecord catchBallRecord) throws Exception {
        if (catchBallRecord.getRight() == null || catchBallRecord.getRight().equals("")){
            catchBallRecord.setRight("true");
        }

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        CatchBallRecordVo catchBallRecordVo = new CatchBallRecordVo();
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);//7天
        Date dBefore = calendar.getTime();
        calendar.add(Calendar.MONTH, -1);//1个月
        Date oneMoon = calendar.getTime();
        calendar.add(Calendar.MONTH, -3);//3个月
        Date threeMoon = calendar.getTime();

        Patient patient = patientDao.needBallCount(catchBallRecord.getIdCard());
        if (patient != null){
            catchBallRecord.setCatchBallRecordPatientId(patient.getPatientId());
            catchBallRecord.setCreationTime(date);
            catchBallRecord.setHeartInfo("{\"heart\":" + JSON.toJSONString(catchBallRecord.getHeart()) + "}");
            catchBallRecord.setBloodSaturationInfo("{\"bloodSaturation\":" + JSON.toJSONString(catchBallRecord.getBloodSaturation()) + "}");
            catchBallRecord.setHrvInfo("{\"hrv\":" + JSON.toJSONString(catchBallRecord.getHrv()) + "}");
            catchBallRecord.setBloodAreaInfo("{\"bloodArea\":" + JSON.toJSONString(catchBallRecord.getBloodArea()) + "}");
            int i = catchBallRecordDao.insertRecord(catchBallRecord);
            if (i <= 0){
                throw new Exception("添加就失败");
            }

            if (catchBallRecord.getCatchBallRecordDetailsList() != null && catchBallRecord.getCatchBallRecordDetailsList().size() > 0){
                for (CatchBallRecordDetails catchBallRecordDetails : catchBallRecord.getCatchBallRecordDetailsList()) {
                    catchBallRecordDetails.setCatchBallRecordId(catchBallRecord.getCatchBallRecordId());
                    i = catchBallRecordDetailsDao.insertRecord(catchBallRecordDetails);
                    if (i <= 0){
                        throw new Exception("添加患者抓小球详情失败");
                    }
                }
            }

            Exerciserecord exerciserecord = new Exerciserecord();
            exerciserecord.setPatientId(patient.getPatientId());
            exerciserecord.setDuration(catchBallRecord.getExerciseDuration());
            exerciserecord.setDate(format.format(new Date()));
            exerciserecord.setActionType(PublicConstants.RecordEnum.SmallBall.getValue());
            exerciserecord.setPrescriptionName("抓小球");
            exerciserecord.setActionTypeId(catchBallRecord.getCatchBallRecordId());
            int insert = exerciserecordDao.insertExerciserecord(exerciserecord);
            if (insert <= 0){
                throw new Exception("添加患者运动总记录失败");
            }

            Date time = new Date();
            Date endTime= new Date(time.getTime()+1000 * 60 * 60 * 24*1);

            List<CatchBallRecord> catchBallRecordList = catchBallRecordDao.selCatchBallRecord(endTime, dBefore, patient.getPatientId());
            if (catchBallRecordList != null && catchBallRecordList.size() > 0){
                for (CatchBallRecord ballRecord : catchBallRecordList) {
                    List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                    if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                        ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                    }
                }
            }
            catchBallRecordVo.setSevenDay(catchBallRecordList);

            List<CatchBallRecord> catchBallRecords = catchBallRecordDao.selCatchBallRecord(endTime, oneMoon, patient.getPatientId());
            if (catchBallRecords != null && catchBallRecords.size() > 0){
                for (CatchBallRecord ballRecord : catchBallRecords) {
                    List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                    if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                        ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                    }
                }
            }
            catchBallRecordVo.setOneMoon(catchBallRecords);

            List<CatchBallRecord> three = catchBallRecordDao.selCatchBallRecord(endTime, threeMoon, patient.getPatientId());
            if (three != null && three.size() > 0){
                for (CatchBallRecord ballRecord : three) {
                    List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                    if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                        ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                    }
                }
            }
            catchBallRecordVo.setThreeMoon(three);
            return catchBallRecordVo;
        }
        else {
            throw new Exception("证件号错误");
        }
    }

    /**
     * 抓小球记录图表
     * */
    @Override
    public CatchBallRecord record(Integer id) {
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -7);//7天
        Date dBefore = calendar.getTime();
        calendar.add(Calendar.MONTH, -1);//1个月
        Date oneMoon = calendar.getTime();
        calendar.add(Calendar.MONTH, -3);//3个月
        Date threeMoon = calendar.getTime();
        Date endTime= new Date(date.getTime()+1000 * 60 * 60 * 24*1);

        CatchBallRecord catchBallRecord = catchBallRecordDao.catchBallRecordById(id);

        List<CatchBallRecord> catchBallRecordList = catchBallRecordDao.selCatchBallRecord(endTime, dBefore, catchBallRecord.getCatchBallRecordPatientId());
        if (catchBallRecordList != null && catchBallRecordList.size() > 0){
            for (CatchBallRecord ballRecord : catchBallRecordList) {
                List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                    ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                }
            }
        }
        catchBallRecord.setSevenDay(catchBallRecordList);

        List<CatchBallRecord> catchBallRecords = catchBallRecordDao.selCatchBallRecord(endTime, oneMoon, catchBallRecord.getCatchBallRecordPatientId());
        if (catchBallRecords != null && catchBallRecords.size() > 0){
            for (CatchBallRecord ballRecord : catchBallRecords) {
                List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                    ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                }
            }
        }
        catchBallRecord.setOneMoon(catchBallRecords);

        List<CatchBallRecord> three = catchBallRecordDao.selCatchBallRecord(endTime, threeMoon, catchBallRecord.getCatchBallRecordPatientId());
        if (three != null && three.size() > 0){
            for (CatchBallRecord ballRecord : three) {
                List<CatchBallRecordDetails> catchBallRecordDetails = catchBallRecordDetailsDao.selCatchBallRecordDetails(ballRecord.getCatchBallRecordId());
                if (catchBallRecordDetails != null && catchBallRecordDetails.size() > 0){
                    ballRecord.setCatchBallRecordDetailsList(catchBallRecordDetails);
                }
            }
        }
        catchBallRecord.setThreeMoon(three);
        if (StringHelper.isNotEmpty(catchBallRecord.getHeartInfo())){
            JSONObject jsonHeart = JSONObject.parseObject(catchBallRecord.getHeartInfo());
            String versionHeart = jsonHeart.getString("heart");
            List<IdNameInfoVo> heartList = JSON.parseArray(versionHeart, IdNameInfoVo.class);
            if (heartList != null && heartList.size() > 0){
                heartList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                for (IdNameInfoVo idNameInfoVo : heartList) {
                    idNameInfoVo.setHighestValue(heartList.get(heartList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(heartList.get(0).getValue());
                }
            }
            catchBallRecord.setHeart(heartList);
        }
        if (StringHelper.isNotEmpty(catchBallRecord.getBloodSaturationInfo())){
            JSONObject jsonBloodSaturation = JSONObject.parseObject(catchBallRecord.getBloodSaturationInfo());
            String versionBloodSaturation = jsonBloodSaturation.getString("bloodSaturation");
            List<IdNameInfoVo> bloodSaturationList = JSON.parseArray(versionBloodSaturation, IdNameInfoVo.class);
            if (bloodSaturationList != null && bloodSaturationList.size() > 0){
                bloodSaturationList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                for (IdNameInfoVo idNameInfoVo : bloodSaturationList) {
                    idNameInfoVo.setHighestValue(bloodSaturationList.get(bloodSaturationList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(bloodSaturationList.get(0).getValue());
                }
            }
            catchBallRecord.setBloodSaturation(bloodSaturationList);
        }
        if (StringHelper.isNotEmpty(catchBallRecord.getHrvInfo())){
                JSONObject jsonHrv = JSONObject.parseObject(catchBallRecord.getHrvInfo());
                String versionHrv = jsonHrv.getString("hrv");
                List<IdNameInfoVo> hrvList = JSON.parseArray(versionHrv, IdNameInfoVo.class);
                if (hrvList != null && hrvList.size() > 0){
                    hrvList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                    for (IdNameInfoVo idNameInfoVo : hrvList) {
                        idNameInfoVo.setHighestValue(hrvList.get(hrvList.size() - 1).getValue());
                        idNameInfoVo.setLowestValue(hrvList.get(0).getValue());
                    }
                }
                catchBallRecord.setHrv(hrvList);
        }
        if (StringHelper.isNotEmpty(catchBallRecord.getBloodAreaInfo())){
            JSONObject jsonBloodArea = JSONObject.parseObject(catchBallRecord.getBloodAreaInfo());
            String versionBloodArea = jsonBloodArea.getString("bloodArea");
            List<IdNameInfoVo> bloodAreaList = JSON.parseArray(versionBloodArea, IdNameInfoVo.class);
            if (bloodAreaList != null && bloodAreaList.size() > 0){
                bloodAreaList.sort((x, y) -> Double.compare(x.getValue(), y.getValue()));
                for (IdNameInfoVo idNameInfoVo : bloodAreaList) {
                    idNameInfoVo.setHighestValue(bloodAreaList.get(bloodAreaList.size() - 1).getValue());
                    idNameInfoVo.setLowestValue(bloodAreaList.get(0).getValue());
                }
            }
            catchBallRecord.setBloodArea(bloodAreaList);
        }


        return catchBallRecord;
    }
}